﻿using System;
using System.ComponentModel;

namespace ExFlash.Records
{
    public class StyleChangeRecord : ShapeRecord
    {
        private readonly ShapeType _shapeType;
        
        public Boolean StateNewStyles
        {
            get;
            protected set;
        }

        public Boolean StateLineStyle
        {
            get;
            protected set;
        }

        public Boolean StateFillStyle1
        {
            get;
            protected set;
        }

        public Boolean StateFillStyle0
        {
            get;
            protected set;
        }

        public Boolean StateMoveTo
        {
            get;
            protected set;
        }

        [Browsable(false)]
        public Byte MoveBits
        {
            get;
            protected set;
        }

        public int MoveDeltaX
        {
            get;
            protected set;
        }

        public int MoveDeltaY
        {
            get;
            protected set;
        }

        public UInt32 FillStyle0
        {
            get;
            protected set;
        }

        public UInt32 FillStyle1
        {
            get;
            protected set;
        }

        public UInt32 LineStyle
        {
            get;
            protected set;
        }

        public FillStyleArray FillStyles
        {
            get;
            protected set;
        }

        public LineStyleArray LineStyles
        {
            get;
            protected set;
        }

        [Browsable(false)]
        public Byte NumFillBits
        {
            get;
            protected set;
        }

        [Browsable(false)]
        public Byte NumLineBits
        {
            get;
            protected set;
        }

        [Browsable(false)]
        public Byte FillBits
        {
            get;
            protected set;
        }

        [Browsable(false)]
        public Byte LineBits
        {
            get;
            protected set;
        }

        public StyleChangeRecord(Byte fillbits, Byte linebits, ShapeType type)
            : base(false)
        {
            FillBits = fillbits;
            LineBits = linebits;
            _shapeType = type;
        }
                
        public override void SwfDeserialize(BinaryBitReader reader)
        {
            StateNewStyles = reader.ReadBoolean();

            StateLineStyle = reader.ReadBoolean();

            StateFillStyle1 = reader.ReadBoolean();

            StateFillStyle0 = reader.ReadBoolean();

            StateMoveTo = reader.ReadBoolean();
                    
            if (StateMoveTo)
            {
                MoveBits = (Byte)reader.ReadUInt32(5);

                MoveDeltaX = reader.ReadInt32(MoveBits);

                MoveDeltaY = reader.ReadInt32(MoveBits);
            }

            if (StateFillStyle0)
            {
                FillStyle0 = reader.ReadUInt32(FillBits);
            }

            if (StateFillStyle1)
            {
                FillStyle1 = reader.ReadUInt32(FillBits);
            }

            if (StateLineStyle)
            {
                LineStyle = reader.ReadUInt32(LineBits);
            }

            if (StateNewStyles)
            {
                reader.AlignBits();

                FillStyles = new FillStyleArray(_shapeType);
                FillStyles.SwfDeserialize(reader);

                LineStyles = new LineStyleArray(_shapeType);
                LineStyles.SwfDeserialize(reader);

                NumFillBits = (Byte) reader.ReadUInt32(4);
                NumLineBits = (Byte) reader.ReadUInt32(4);
            }
            
        }

        public override void SwfSerialize(byte[] arr, ref int offset)
        {
            throw new NotImplementedException();
        }
    }
}
